package com.gitee.wsl.time

import kotlin.time.DurationUnit
import kotlin.time.toDuration

typealias TimeUnit = DurationUnit


 val DurationUnit.shortName: String get() = when (this) {
    DurationUnit.NANOSECONDS -> "ns"
    DurationUnit.MICROSECONDS -> "us"
    DurationUnit.MILLISECONDS -> "ms"
    DurationUnit.SECONDS -> "s"
    DurationUnit.MINUTES -> "m"
    DurationUnit.HOURS -> "h"
    DurationUnit.DAYS -> "d"
    else -> error("Unknown unit: $this")
}

 val DurationUnit.shortNameSquared: String get() = when (this) {
    DurationUnit.NANOSECONDS -> "ns²"
    DurationUnit.MICROSECONDS -> "us²"
    DurationUnit.MILLISECONDS -> "ms²"
    DurationUnit.SECONDS -> "s²"
    DurationUnit.MINUTES -> "m²"
    DurationUnit.HOURS -> "h²"
    DurationUnit.DAYS -> "d²"
    else -> error("Unknown unit: $this")
}

 val DurationUnit.secondScale: Double get() = when (this) {
    DurationUnit.NANOSECONDS -> 0.000_000_001
    DurationUnit.MICROSECONDS -> 0.000_001
    DurationUnit.MILLISECONDS -> 0.001
    DurationUnit.SECONDS -> 1.0
    DurationUnit.MINUTES -> 60.0
    DurationUnit.HOURS -> 3_600.0
    DurationUnit.DAYS -> 86_400.0
    else -> error("Unknown unit: $this")
}

fun TimeUnit.toNanos(value:Long) = value.toDuration(this).inWholeNanoseconds
fun TimeUnit.toNanos(value:Int) = value.toDuration(this).inWholeNanoseconds

fun TimeUnit.toMillis(value:Long) = value.toDuration(this).inWholeMilliseconds
fun TimeUnit.toMillis(value:Int) = value.toDuration(this).inWholeMilliseconds

fun TimeUnit.toSeconds(value:Long) = value.toDuration(this).inWholeSeconds
fun TimeUnit.toSeconds(value:Int) = value.toDuration(this).inWholeSeconds

fun TimeUnit.toMinutes(value:Long) = value.toDuration(this).inWholeMinutes
fun TimeUnit.toMinutes(value:Int) = value.toDuration(this).inWholeMinutes

fun TimeUnit.toHours(value:Long) = value.toDuration(this).inWholeHours
fun TimeUnit.toHours(value:Int) = value.toDuration(this).inWholeHours

fun TimeUnit.convert(value:Long,baseUnit: TimeUnit) = value.toDuration(baseUnit).toLong(this)
fun TimeUnit.convert(value:Int,baseUnit: TimeUnit) = value.toDuration(baseUnit).toLong(this)